package vn.wallet.app.dao.impl;

// Generated Oct 23, 2012 10:05:44 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import vn.wallet.app.dao.IProfileDAO;
import vn.wallet.app.domain.Profile;

@Repository
public class ProfileDAO extends GenericDAO<Profile> implements IProfileDAO {

	public ProfileDAO() {
		super(Profile.class);
	}

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Profile getById(int id) throws Exception {
		return (Profile) getSessionFactory().getCurrentSession().get(Profile.class, id);
	}

	@Override
	public List<Profile> getByName(String name) throws Exception {
		return null;
	}

	@Override
	public void save(Profile obj) throws Exception {
		getSessionFactory().getCurrentSession().persist(obj);
	}

	@Override
	public void update(Profile obj) throws Exception {
		getSessionFactory().getCurrentSession().merge(obj);
	}

	@Override
	public void delete(Profile obj) throws Exception {
		getSessionFactory().getCurrentSession().delete(obj);
	}

	@Override
	public void delete(int id) throws Exception {
		getSessionFactory().getCurrentSession().delete(getById(id));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Profile> getAll() throws Exception {
		return getSessionFactory().getCurrentSession().createQuery("from Profile").list();
	}

	@Override
	public int getCount() throws Exception {
		return (Integer) getSessionFactory().getCurrentSession().createQuery("select count(*) from Profile").uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Profile> findByExample(Profile obj) throws Exception {
		List<Profile> lst = null;
		Example ex = Example.create(obj);
		ex.enableLike(MatchMode.ANYWHERE);
		ex.ignoreCase();

		lst = getSessionFactory().getCurrentSession().createCriteria(Profile.class)
				.add(ex)
				.list();

		return lst;
	}

	@Override
	public List<Profile> search() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
